package com.zut.hospital.controller.login;

//import com.zut.hospital.config.ResultMap;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.zut.hospital.controller.pharmacy.ExcelImportController;
import com.zut.hospital.dao.staff.UserMapper;
//import com.howie.shiro.model.ResultMap;
import com.zut.hospital.entity.staff.User;
import com.zut.hospital.entity.staff.UserExample;
import com.zut.hospital.service.login.LoginService;
//import org.apache.shiro.SecurityUtils;
//import org.apache.shiro.authc.UsernamePasswordToken;
//import org.apache.shiro.subject.Subject;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.text.ParseException;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @program: hos-test
 * @description:
 * @author: 杨曼珍
 * @create: 2019-07-05 17:41
 **/
@Controller
@RequestMapping(value = "/hospital")
public class LoginController {
    @Autowired
    HttpServletRequest request;
    @Autowired
    HttpServletResponse response;
    @Autowired
    LoginService loginService;
    private static final Logger log = LoggerFactory.getLogger(LoginController.class);
    public String layuiJsonParse(List<Map<String, String>> list, int size) {
        Map<String, Object> layuiMap = new HashMap<>();
        layuiMap.put("code", 1);
        layuiMap.put("msg", "success");
        layuiMap.put("count", size);
        layuiMap.put("data", list);
        return JSON.toJSONString(layuiMap);
    }

    @ResponseBody
    @RequestMapping(value = "/login",method = RequestMethod.POST)
    public String loginDemo(@RequestBody String data,HttpSession session,ModelMap modelMap){
        String userNum = JSONObject.parseObject(data).getString("username");
        String pwd = JSONObject.parseObject(data).getString("password");
        String adminUser=userNum;
        if (loginService.loginJudge(userNum,pwd).equals(1)){
            session.setAttribute("adminUser",adminUser);
            Cookie cookie=new Cookie("JSESSIONID", session.getId());
            //设置cookie保存时间
            cookie.setMaxAge(60*20);
            //被创建的cookie返回浏览器
            response.addCookie(cookie);
            modelMap.addAttribute("UserName",userNum);

            Map<String, Object> layuiMap = new HashMap<>();
            layuiMap.put("code", 1);
            layuiMap.put("msg", "success");
            layuiMap.put("count", 0);
            layuiMap.put("data", "");
            return JSON.toJSONString(layuiMap);
        }
        Map<String, Object> layuiMap = new HashMap<>();
        layuiMap.put("code", 0);
        layuiMap.put("msg", "field");
        layuiMap.put("count", 0);
        layuiMap.put("data", "");
        log.info("session"+session.getAttribute("adminUser"));
        System.out.println("session"+session.getAttribute("adminUser"));
        return JSON.toJSONString(layuiMap);

    }

    @ResponseBody
    @RequestMapping(value = "/userPower",method = RequestMethod.POST)
    public String userPower(HttpSession session){
        String name=session.getAttribute("adminUser").toString();
          int a=loginService.userPower(name);
          if (a==1){
            Map<String, Object> layuiMap = new HashMap<>();
            layuiMap.put("code", 1);
            layuiMap.put("msg", "success");
            layuiMap.put("count", 0);
            layuiMap.put("data", "");
            return JSON.toJSONString(layuiMap);
        }
        Map<String, Object> layuiMap = new HashMap<>();
        layuiMap.put("code", 0);
        layuiMap.put("msg", "failed");
        layuiMap.put("count", 0);
        layuiMap.put("data", "");
        return JSON.toJSONString(layuiMap);

    }
    @ResponseBody
    @RequestMapping(value = "/countpatAlll")
    public String countpatAlll(){
        return loginService.countgua();

    }
    @ResponseBody
    @RequestMapping(value = "/tu")
    public String tu()throws ParseException {
        return loginService.tu(new Date());

    }

}